import Vue from 'vue'
import VueRouter from 'vue-router'
import DataRouter from './datastatistics'
import CardNumberManagementRouter from './cardnumbermanagement'
import FinancialManagementRouter from './financialmanagement'
import SystemManagementRouter from './systemmanagement'
import BigCustomerRouter from './bigcustomer'
import login from "../views/Login/Login.vue"
import home from "../views/index/Index.vue"
import {getApiroot} from "../api/roleRoot"
Vue.use(VueRouter)

const routes = [
  {
    path:'',
    redirect:'/login'
  },
  { // 登录
    path: '/login',
    name: 'login',
    meta:{
      title:"登录"
    },
    component: login,
  },
  {
    path: '/provincialsales',
    name:'home',
    meta:{
      title:"省统计"
    },
    component: home,
    children:[
    
   ...DataRouter,
  ...CardNumberManagementRouter,
  ...FinancialManagementRouter,
  ...BigCustomerRouter,
  ...SystemManagementRouter,
    ]
   
  },
  
]




const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log(to,from,next);
     if(to.meta.title){
       document.title=to.meta.title
       next()
     }
  let userMsg = localStorage.userInfo? JSON.parse(localStorage.userInfo) : '';
  if(to.path=='/login'){
    next()
  }else{
    if(to.path=='/login'||userMsg){
      
      getApiroot({
        id:userMsg.root
      }).then(res=>{
      
        let obj={
          userImg:userMsg.userImg,
          username:userMsg.username,
          root:userMsg.root,
          nickname:userMsg.nickname
        }
        router.app.$store.commit("getUsermsg",obj)
        router.app.$store.commit("getUserRoot", res.data)
      })
      next()


      }else{
     
     alert('请登录')
     next('/login')// 指定去哪个路由

}
  }
})




export default router
